#include "dds_com.h"
#include "dds_ctl.h"
#include <stdio.h>
#include "ff.h"
#include "diskio.h"


//#define coePath "0:_sys/sys.coe"
#define coe_P2M_Path "0:_sys/p2m.coe"
#define coe_M2P_Path "0:_sys/m2p.coe"
#define coe_Path		 "0:_sys/sys.coe"
#define pac_Path		 "0:_sys/sys.pac"

extern unsigned char MCU_READ_Flag;

//FATFS COEfile;
FATFS fs;
DIR dirs;
FIL sysFile;
//FIL coe_M2P;
unsigned int br;
char fatfr;

unsigned char _isConnected = 0;

void USB_Interrupts_Enable(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  NVIC_InitStructure.NVIC_IRQChannel = USB_HP_CAN1_TX_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

void USB_Interrupts_Disable(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
  NVIC_Init(&NVIC_InitStructure);

  NVIC_InitStructure.NVIC_IRQChannel = USB_HP_CAN1_TX_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
  NVIC_Init(&NVIC_InitStructure);
}

void communicate_init(void)
{
	if(f_mount(0,&fs) == FR_OK)
	{
		//printf("\r\nMount OK!");
	}
	else
	{
		printf("\r\nMount Fail!");
		return;
	}
	
	//.coe init
	printf("\r\nFile init ...");
	if((fatfr = f_open(&sysFile, coe_Path , FA_OPEN_EXISTING | FA_WRITE)) == FR_OK)
	{
		//printf("\r\nInit .coe File ... ");
	}
	if((fatfr = f_write(&sysFile,"00",2,&br)) == FR_OK)
		printf("OK!\n");
	f_close(&sysFile);
}

void connect_est()
{
	//unsigned int br;
	//char coePath[] = "0:_sys/sys.coe";
	//char fatfr;
	char coeReadbuf[]="00";
	//unsigned int i=0;
	
	/*USB_Interrupts_Disable();
	printf("Open .coe File ... ");
	if((fatfr = f_open(&coeFile, coePath , FA_OPEN_EXISTING | FA_READ)) == FR_OK)
	{
		printf("OK!\n");
	}
	else
	{
		printf("Fail: %d",fatfr);
		USB_Interrupts_Enable();
		return;
	}
	USB_Interrupts_Enable();*/
	
	printf("\r\nWaiting for PC responding");
	while((coeReadbuf[0] != '1') || (coeReadbuf[1] != '0'))	//waiting for PC responding
	{
		USB_Interrupts_Disable();
		f_open(&sysFile, coe_Path , FA_OPEN_EXISTING | FA_READ);
		f_read(&sysFile,coeReadbuf,2,&br);
		//printf("%d Read %c %c\n",i++,coeReadbuf[0],coeReadbuf[1]);
		f_close(&sysFile);
		USB_Interrupts_Enable();
		delay_ms(200);
	}
	printf("\r\nDeceted PC!!");
	
	/*MCU responding*/
	/*while(1)
	{
		delay_ms(500);
		USB_Interrupts_Disable();
		f_open(&coe_M2P, coe_M2P_Path , FA_OPEN_EXISTING | FA_WRITE);
		if((fatfr = f_write(&coe_M2P,"11",2,&br)) == FR_OK)
		//	if((fatfr = f_write(&coeFile,"123",4,&br)) == FR_OK)
		{
			printf("Writing ..\n");
		}
		//f_close(&coeFile);
		f_sync(&coe_M2P);
		f_close(&coe_M2P);
		USB_Interrupts_Enable();
		
		delay_ms(10);
		USB_Interrupts_Disable();
		f_open(&coe_M2P, coe_M2P_Path , FA_OPEN_EXISTING | FA_READ);
		f_read(&coe_M2P,coeReadbuf,2,&br);
		printf("Read %c %c\n",coeReadbuf[0],coeReadbuf[1]);
		if((coeReadbuf[0] == '1') || (coeReadbuf[1] == '1'))
		{
			printf("Connecting Success!\n");
			f_close(&coe_M2P);
			USB_Interrupts_Enable();
			_isConnected = 1;
			return;
		}
		f_close(&coe_M2P);
		USB_Interrupts_Enable();
	}*/
}

int Connect_Detection(void)
{
	char coeReadbuf[]="00";
	
	USB_Interrupts_Disable();
	f_open(&sysFile, coe_Path , FA_OPEN_EXISTING | FA_READ);
	f_read(&sysFile,coeReadbuf,2,&br);
	f_close(&sysFile);
	USB_Interrupts_Enable();
	if((coeReadbuf[0] == '1') && (coeReadbuf[1] == '0'))
	{
		_isConnected = 1;
		return 1;
	}
	else if((coeReadbuf[0] == '0') && (coeReadbuf[1] == '0'))
	{
		_isConnected = 0;
		return 0;
	}
	else
		return 2;//Error!
}

int pacFileRead(char *pacdat)
{
	USB_Interrupts_Disable();
	
	if((fatfr = f_open(&sysFile, pac_Path , FA_OPEN_EXISTING | FA_READ)) == FR_OK)
	{
		if((fatfr = f_read(&sysFile,pacdat,36,&br)) != FR_OK)
		{
			printf("\r\nread pac file error");
			f_close(&sysFile);		
			USB_Interrupts_Enable();
			return -2;
		}

	}
	else
	{
		printf("\r\nOpen pac file error");
		USB_Interrupts_Enable();
		return -1;
	}
	//printf("\r\nRead pac File OK!");
	f_close(&sysFile);
	USB_Interrupts_Enable();
	
	return 0;
}

void pacFileWrite(void)
{
	unsigned char pacwr[17]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};
	
	USB_Interrupts_Enable();
	
	f_open(&sysFile, pac_Path , FA_OPEN_EXISTING | FA_WRITE);
	f_write(&sysFile,pacwr,17,&br);
	printf("\r\nWrite pac File OK!");
	f_close(&sysFile);

	USB_Interrupts_Disable();
}

